﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;
    using System.Data;

    internal class Code93 : BarcodeCommon, IBarcode
    {
        private DataTable C93_Code = new DataTable("C93_Code");

        public Code93(string input)
        {
            base.Raw_Data = input;
        }

        private string Add_CheckDigits(string input)
        {
            int num2;
            char ch;
            int[] numArray = new int[input.Length];
            int num = 1;
            for (num2 = input.Length - 1; num2 >= 0; num2--)
            {
                if (num > 20)
                {
                    num = 1;
                }
                numArray[num2] = num;
                num++;
            }
            int[] numArray2 = new int[input.Length + 1];
            num = 1;
            for (num2 = input.Length; num2 >= 0; num2--)
            {
                if (num > 15)
                {
                    num = 1;
                }
                numArray2[num2] = num;
                num++;
            }
            int num3 = 0;
            for (num2 = 0; num2 < input.Length; num2++)
            {
                ch = input[num2];
                num3 += numArray[num2] * int.Parse(this.C93_Code.Select("Character = '" + ch.ToString() + "'")[0]["Value"].ToString());
            }
            int num4 = num3 % 0x2f;
            input = input + this.C93_Code.Select("Value = '" + num4.ToString() + "'")[0]["Character"].ToString();
            num3 = 0;
            for (num2 = 0; num2 < input.Length; num2++)
            {
                ch = input[num2];
                num3 += numArray2[num2] * int.Parse(this.C93_Code.Select("Character = '" + ch.ToString() + "'")[0]["Value"].ToString());
            }
            num4 = num3 % 0x2f;
            input = input + this.C93_Code.Select("Value = '" + num4.ToString() + "'")[0]["Character"].ToString();
            return input;
        }

        private string Encode_Code93()
        {
            this.init_Code93();
            string str = this.Add_CheckDigits(base.Raw_Data);
            string str2 = this.C93_Code.Select("Character = '*'")[0]["Encoding"].ToString();
            foreach (char ch in str)
            {
                try
                {
                    str2 = str2 + this.C93_Code.Select("Character = '" + ch.ToString() + "'")[0]["Encoding"].ToString();
                }
                catch
                {
                    throw new Exception("EC93-1: Invalid data.");
                }
            }
            str2 = str2 + this.C93_Code.Select("Character = '*'")[0]["Encoding"].ToString() + "1";
            this.C93_Code.Clear();
            return str2;
        }

        private void init_Code93()
        {
            this.C93_Code.Rows.Clear();
            this.C93_Code.Columns.Clear();
            this.C93_Code.Columns.Add("Value");
            this.C93_Code.Columns.Add("Character");
            this.C93_Code.Columns.Add("Encoding");
            this.C93_Code.Rows.Add(new object[] { "0", "0", "100010100" });
            this.C93_Code.Rows.Add(new object[] { "1", "1", "101001000" });
            this.C93_Code.Rows.Add(new object[] { "2", "2", "101000100" });
            this.C93_Code.Rows.Add(new object[] { "3", "3", "101000010" });
            this.C93_Code.Rows.Add(new object[] { "4", "4", "100101000" });
            this.C93_Code.Rows.Add(new object[] { "5", "5", "100100100" });
            this.C93_Code.Rows.Add(new object[] { "6", "6", "100100010" });
            this.C93_Code.Rows.Add(new object[] { "7", "7", "101010000" });
            this.C93_Code.Rows.Add(new object[] { "8", "8", "100010010" });
            this.C93_Code.Rows.Add(new object[] { "9", "9", "100001010" });
            this.C93_Code.Rows.Add(new object[] { "10", "A", "110101000" });
            this.C93_Code.Rows.Add(new object[] { "11", "B", "110100100" });
            this.C93_Code.Rows.Add(new object[] { "12", "C", "110100010" });
            this.C93_Code.Rows.Add(new object[] { "13", "D", "110010100" });
            this.C93_Code.Rows.Add(new object[] { "14", "E", "110010010" });
            this.C93_Code.Rows.Add(new object[] { "15", "F", "110001010" });
            this.C93_Code.Rows.Add(new object[] { "16", "G", "101101000" });
            this.C93_Code.Rows.Add(new object[] { "17", "H", "101100100" });
            this.C93_Code.Rows.Add(new object[] { "18", "I", "101100010" });
            this.C93_Code.Rows.Add(new object[] { "19", "J", "100110100" });
            this.C93_Code.Rows.Add(new object[] { "20", "K", "100011010" });
            this.C93_Code.Rows.Add(new object[] { "21", "L", "101011000" });
            this.C93_Code.Rows.Add(new object[] { "22", "M", "101001100" });
            this.C93_Code.Rows.Add(new object[] { "23", "N", "101000110" });
            this.C93_Code.Rows.Add(new object[] { "24", "O", "100101100" });
            this.C93_Code.Rows.Add(new object[] { "25", "P", "100010110" });
            this.C93_Code.Rows.Add(new object[] { "26", "Q", "110110100" });
            this.C93_Code.Rows.Add(new object[] { "27", "R", "110110010" });
            this.C93_Code.Rows.Add(new object[] { "28", "S", "110101100" });
            this.C93_Code.Rows.Add(new object[] { "29", "T", "110100110" });
            this.C93_Code.Rows.Add(new object[] { "30", "U", "110010110" });
            this.C93_Code.Rows.Add(new object[] { "31", "V", "110011010" });
            this.C93_Code.Rows.Add(new object[] { "32", "W", "101101100" });
            this.C93_Code.Rows.Add(new object[] { "33", "X", "101100110" });
            this.C93_Code.Rows.Add(new object[] { "34", "Y", "100110110" });
            this.C93_Code.Rows.Add(new object[] { "35", "Z", "100111010" });
            this.C93_Code.Rows.Add(new object[] { "36", "-", "100101110" });
            this.C93_Code.Rows.Add(new object[] { "37", ".", "111010100" });
            this.C93_Code.Rows.Add(new object[] { "38", " ", "111010010" });
            this.C93_Code.Rows.Add(new object[] { "39", "$", "111001010" });
            this.C93_Code.Rows.Add(new object[] { "40", "/", "101101110" });
            this.C93_Code.Rows.Add(new object[] { "41", "+", "101110110" });
            this.C93_Code.Rows.Add(new object[] { "42", "%", "110101110" });
            this.C93_Code.Rows.Add(new object[] { "43", "(", "110101110" });
            this.C93_Code.Rows.Add(new object[] { "44", ")", "110101110" });
            this.C93_Code.Rows.Add(new object[] { "45", "#", "110101110" });
            this.C93_Code.Rows.Add(new object[] { "46", "@", "110101110" });
            this.C93_Code.Rows.Add(new object[] { "-", "*", "101011110" });
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_Code93();
            }
        }
    }
}

